/**
 * created by 小卷毛, 2020-05-07 007
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.aifeng.viewmodel

import com.aifeng.model.BaseUrl
import com.aifeng.model.CommonData
import com.aifeng.utils.applyFlowableSchedulers
import com.aifeng.utils.applySchedulers
import com.lzg.extend.BaseResponse
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.RxSingleHelper
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo

/**
 * 项目名称：Hassan_Food
 * 创建人：小卷毛
 * 创建时间：2020-05-07 17:41
 */
class HomeViewModel : NoticeViewModel() {

    fun addToCar(
        id: String,
        userId: String,
        num: Int
    ) =
        RxSingleHelper.requestString(
            url = BaseUrl.addCar,
            params = getParams(
                "goodsid" to id,
                "userid" to userId,
                "num" to num
            )
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    fun banner() =
        OkGo.post<BaseResponse<ArrayList<CommonData>>>(BaseUrl.banner)
            .converter(object : JacksonConvert<BaseResponse<ArrayList<CommonData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }

    fun getHotList() =
        OkGo.post<BaseResponse<ArrayList<CommonData>>>(BaseUrl.goodsHot)
            .converter(object : JacksonConvert<BaseResponse<ArrayList<CommonData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }

    fun getRecommendList(userId: String) =
        OkGo.post<BaseResponse<ArrayList<CommonData>>>(BaseUrl.goodsRecommend)
            .apply {
                if (userId.isNotEmpty()) {
                    params(getParams("userid" to userId))
                }
            }
            .converter(object : JacksonConvert<BaseResponse<ArrayList<CommonData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }

}